<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_value_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_value_deleter.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_attr_types/tcms_cia_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attr_type_dbc.class.php');


class Nomenclature_Value_Del_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_nomenclature/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete', Tangra_Parameter_Method::GET, 'del'), 'delete_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete_confirmed', Tangra_Parameter_Method::GET, 'del_confirmed'), 'delete_confirmed');
	}


	protected function delete_event($nomenclature_value_id) {
		$dbc = $this->get_dbc();

		$nomenclature_value = new TCMS_Nomenclature_Value_DBC();
		if ($nomenclature_value->load_by_id($dbc, $nomenclature_value_id)) {
			$this->export('nomenclature_value', $nomenclature_value);
			$nomenclature = new TCMS_Nomenclature_DBC();
			if (!$nomenclature->load_by_id($dbc, $nomenclature_value->get_nomenclature())) {
				throw new Tangra_Module_Exception('Cannot load TCMS_Nomenclature_DBC with id = '.$nomenclature_value->get_nomenclature());
			}
			$this->export('nomenclature_name', $nomenclature->get_name());
			$this->export('nomenclature_id', $nomenclature->get_id());

			$nomenclature_type = new TCMS_CI_Attr_Type_DBC();
			if (!$nomenclature_type->load_by_hid($dbc, 'nomenclature')) {
				throw new Tangra_Module_Exception('Cannot load TCMS_CI_Attr_Type_DBC with hid = "nomenclature"');
			}
			$count = TCMS_CIA_Nomenclature_DBC::count_all_for_nom($dbc,
																	$nomenclature_type->get_id(),
																	$nomenclature->get_hid(),
																	$nomenclature_value_id);
			$this->export('used_count', $count);

			$view = $this->get_view('default');
		} else {
			$view = $this->get_view('not_exist');
		}

		return $view;
	}


	protected function delete_confirmed($nomenclature_value_id) {
		$dbc = $this->get_dbc();

		$nomenclature_value = new TCMS_Nomenclature_Value_DBC();
		if ($nomenclature_value->load_by_id($dbc, $nomenclature_value_id)) {
			try {
				$dbc->start_trans();
				TCMS_Nomenclature_Value_Deleter::delete($dbc,
														$nomenclature_value_id,
														$this->_config->get_hidden_dir().'inc/',
														$this->get_user()->get_id());
				$dbc->complete_trans();
			} catch (Exception $e) {
				$dbc->fail_trans();
				$dbc->complete_trans();
				throw $e;
			}

			$redir = $this->create_redirect_view('admin/modules/tangra_cms/nomenclature/nomenclature_manage.php', array('_transit_nom' => $nomenclature_value->get_nomenclature()));
		} else {
			$redir = $this->create_redirect_view('admin/modules/tangra_cms/nomenclature/nomenclatures.php');
		}

		return $redir;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "del" parameter.');
	}
}